#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <ctype.h>
#include <sys/time.h>

#define MAX_LEN 1024*1024

long long diff_ms(struct timeval t1, struct timeval t2) {
    long long diff;
    if (t2.tv_usec >= t1.tv_usec)
        diff = (t2.tv_usec - t1.tv_usec);
    else {
        diff = (1000000 + t2.tv_usec - t1.tv_usec);
        t2.tv_sec--;
    }
    diff += (t2.tv_sec - t1.tv_sec) * 1000000;  // 转换成微秒
    return diff;  // 转换成毫秒
}

char * get_time() {

   static char arr[50];

   time_t t = time(NULL);

   struct tm *local_time = localtime(&t);

   strftime(arr, sizeof(arr), "%Y-%m-%d %H:%M:%S", local_time);

   return arr;

}

char * get_str(int len) {

    static char str[MAX_LEN];

    for (int i = 0; i < len; i++) {

        int ch = rand() % 62;

        str[i] = (ch < 10) ? '0' + ch : (ch < 36) ? 'A' + ch - 10 : 'a' + ch - 36;

    }

    str[len] = '\0';

    return str;

}

int write_file(char * name, char * str, int loop) {

    FILE *file = fopen(name, "a");

    if (file == NULL) {

        printf("Error opening file!\n");

        return 1;

    }

    struct timeval start, end, start2, end2;

    long long difference,diff = 0;

    gettimeofday(&start, NULL);

    int total_len = 0;

    int count = 100;

    for (int i = 0; i < 1024*1024; i++) {

        for (int j = 0; j < 11; j++) {

            int ch = rand() % 1048400;

            char destination[100];

            total_len = total_len + count;

            if (total_len >= 1024*1024*1024) {

                total_len = total_len - count + 24;

                count = 24;

            }

            for (int k = ch; k < ch + count; k++) {

                destination[k - ch] = str[k];

            }

            destination[count] = '\0';

            gettimeofday(&start2, NULL);

            fputs(destination, file);

            gettimeofday(&end2, NULL);

            diff = diff + diff_ms(start2, end2);
            if (count == 24) {

                break;

            }

        }

        if (count == 24) {

            break;

        }

        fputs("\n", file);

    }

    fclose(file);

    char * now_time = get_time();

    gettimeofday(&end, NULL);

    difference = diff_ms(start, end)/1000;

    diff = diff / 1000;

    printf("%s %d bytes (%d MB) copied, %lld ms, %lld MB/s(%lld MB/s), %s [%d]\n", now_time, total_len, total_len / 1024 / 1024, difference, (1024 * 1000 * 1000 / difference) / 1000,(1024 * 1000 * 1000 / diff)/ 1000, name, loop);

    return 0;
}
 
int f_help(char * program_name) {

    printf("\n\n\n");
	
    printf("Description : \n\n");
	
    printf("    This is a text file generator that defaults to 1GB per file and does not support changing the size. The user can specify a number to tell the tool how many files it needs to generate to fill up the storage device.\n\n\n");
	
    printf("%s OPTIONS : \n\n",program_name);

    printf("    -c  | --count 10   [ Please output a number, the number of files (one file is 1GB) ]\n");

    printf("\n\n\n");

    printf("Synopsis : \n\n");

    printf("    %s -c 10 \n\n", program_name);

    printf("    %s --count 10\n", program_name);

    printf("\n\n\n");

    printf("Please contact me: mo_shan@yeah.net/1005155691 \n\n");

    exit(1);
}

int main(int argc, char *argv[]) {

    srand(time(0));

    if (argc < 2) {

        printf("\nFor more details, use the --help option to view help. \n\n");

        exit(1);

    } else if ( strcmp(argv[1], "--help") == 0 || (strcmp(argv[1], "-c") != 0 && strcmp(argv[1], "--count") != 0)) {

        f_help(argv[0]);

    }

    char * str = get_str(1024*1024);

    int i = 0;

    int num = atoi(argv[2]);

    while (i < num) {

        char * name = get_str(10);

        strcat(name, ".tmp");
 
        write_file(name, str, i + 1);

        i = i + 1;

    }

    return 0;
}
